package com.ecity.view {
	import com.ecity.events.GameEvent;
	import com.ecity.events.GunEvent;
	import com.ecity.table.HitButton;
	import com.ecity.table.ProgressBar;
	import com.ecity.table.PromptField;
	import com.ecity.util.GameLoader;
	import com.ecity.table.Gun;
	import com.ecity.table.Player;
	import com.ecity.util.HashMap;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.setTimeout;
	import flash.utils.clearTimeout;
	import com.ecity.events.GameState;
	
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class MainView extends Sprite {
		
		private const _RPOSI:Array = [[250, 700, 30], [750, 700, -30], [920, 384, -90], [750, 80, 210], [250, 80, 150], [80, 384, 90]];
		
		private var frameIndex:int = 0;
		
		private var gun:Gun;
		private var pmap:HashMap = new HashMap();
		private var _target:Player;
		private var _targetIndex:int = 0;
		private var _stUint:uint;
		private var _shadow:Bitmap;
		private var _isFrist:Boolean;
		private var pop:PromptField;
		private var _button:HitButton;
		
		public function MainView() {
			initResource();
			_button = new HitButton();
		}
		
		private function initResource():void {
			var assetLoder:GameLoader = new GameLoader();
			assetLoder.startLoad(onSourceLoadComplete);
		}
		
		private function onSourceLoadComplete():void {
			if (arguments[0] == GameLoader.INIT_UI) {
				initBackground();
			}else if (arguments[0] == GameLoader.PROGRESS) {
				//_pbar.update(UIFactory.uimap.size()*100/GameLoader.totalSize);
			}else {
				this.dispatchEvent(new GameEvent(GameEvent.RES_COMPLETE));
				this.addChild(_button);
			}
		}
		
		public function mInit():void {
			gun.loadAndSpin();
		}
		
		public function startGame():void {
			_button.addButton("b_startGame");
		}
		
		public function startplay():void {
			initPlayer();
			initGun();
		}
		
		public function spinWheel():void {
			trace("============spinWheel========================");
			gun.loadAndSpin();
			pop = new PromptField();
			this.addChildAt(pop,getChildIndex(_button));
			pop.addEventListener(GunEvent.PUNISH_COMPLETE, onPunished);
		}
		
		public function aimPlayer():void {
			if (_stUint) {
				clearTimeout(_stUint);
				}
			if (_targetIndex != 0) {
				_target.backward();
			}
			_target = pmap.get("p"+_targetIndex);
			gun.moveToPlayer(_target);
			_target.forward();
			_targetIndex++;
		}
		
		public function fire():void {
			gun.onFire();
		}
		
		
		public function finishPlay():void {
			_targetIndex = 0;
			this.addChildAt(pop,getChildIndex(_button));
			_target.backward();
			_target.clearBlood();
		}
		public function hitTestCase(x:int, y:int):void {
			if(GameState.hitable){
				switch(GameState.state) {
					case GameState.GAME_READY:
						if (_button.hitTest(x, y, "b_startGame")) {
							startplay();
						}
						break;
					case GameState.GAME_START:
						if (_button.hitTest(x, y, "b_callSpin")) {
							this.spinWheel();
						}
						break;
						
					case GameState.GAME_AIMED:
						if (_button.hitTest(x, y, "b_callFire")) {
							this.fire();
						}
						break;
				}
			}
		}
		
	/*************-----------------------------------**************/
		private function initBackground():void {
			var bg:Bitmap = UIFactory.getBitmapbyName("bg");
			this.addChildAt(bg,0);
		}
		
		public function initGun():void {
			gun = new Gun();
			this.addChildAt(gun,getChildIndex(_button));
			gun.addEventListener(GunEvent.WHEEL_ISREADY, onGunWheelHandler);
			gun.addEventListener(GunEvent.SPIN_COMPLETE, onGunWheelHandler);
			gun.addEventListener(GunEvent.FIRE_COMPLETE, onFireComplete);
			gun.addEventListener(GunEvent.AIM_COMPLETE, onAimedComplete);
		}
		
		public function initPlayer():void {
			for (var i:int = 0; i < 6; i++) {
				var player:Player = new Player(i);
				pmap.put("p" + i, player);
				player.x = _RPOSI[i][0];
				player.y = _RPOSI[i][1];
				player.rotation = _RPOSI[i][2];
				this.addChildAt(player,getChildIndex(_button));
			}
		}
	/*************-----------------------------------**************/	
		
		private function onFireComplete(e:GunEvent):void {
			var bloodable:Boolean = e._info.isExplode;
			if (bloodable) {
				_target.blood();
				pop.x = _target.x;
				pop.y = _target.y;
				pop.rotation = _target.rotation;
				pop.prompt(["beer", "shot1", "shot2"], 1000, 100, false, true);
				_targetIndex = 0;
			}else {
				_target.surprise();
				_stUint = setTimeout(aimPlayer, 3000);
			}
		}
		
		private function onGunWheelHandler(e:GunEvent):void {
			switch(e.type) {
				case GunEvent.WHEEL_ISREADY:
					GameState.state = GameState.GAME_START;
					_button.addButton("b_callSpin");
					GameState.hitable = true;
					break;
				case GunEvent.SPIN_COMPLETE:
					aimPlayer();
					break;
			}
		}
		private function onPunished(e:GunEvent):void {
			finishPlay();
		}
		
		private function onAimedComplete(e:GunEvent):void {
			_target.tight();
			GameState.state = GameState.GAME_AIMED;
			trace("GameState.stateGameState.state",GameState.state);
			_button.addButton("b_callFire");
			GameState.hitable = true;
		}

	}
}